import { Command } from '../constant';
import _ from 'lodash';
import { IMainReducer } from '../types';
import { Action } from 'typings';
import produce from 'immer';
import * as immerUtil from '@/redux/immer-util';

const INITIAL_STATE: IMainReducer = {
  money: 0,
  isReady: false,
  isLoading: false,
  isLogin: false, // 是否已登录状态
  visible: false,
  money: 0,
  customer: {
    name: "",
    pictureUrl: "",
  },
  orderCount: {
    waitPay: 0,
    waitDeliver: 0,
    waitReceiving: 0,
    waitEvaluate: 0,
    refund: 0,
  },
  rankData:[],
  // propList: [],
  showRankModal: false,
  bottomClickNavigation: false,

  task: {
    items: [],
    page: {
      total: 0,
      pageNum: 0,
      pageSize: 10000,
    },
    detail: {},
  },

  // 用户信息
  userInfo: {
    accountType: '', // 会员信息
    isNewComer: '', // 是否是新人保障
  },

  // 反馈结果
  response: {
    loadUser: '', // 查询用户信息
    loadProduct: '', // 查询许愿列表
    loadGameInfo: '', // 查询游戏信息
    loadGameDaily: '',
    createWish: '', // 许愿
    cancelWish: '', // 取消许愿
    isNewComer: '', // 是否是新人保障
    loadGameLottery: '', // 查询奖品
    claimRewards: '',
    loadTask: '', // 查询任务
    loadTaskDetail: '', // 查询任务详情
    computedTask: '',
    loadProp: '',
    usePropPossibility: '', // 判断用户是否能使用道具
  },

  // 状态反馈
  status: {
    loadingUser: false, // 查询用户信息
    loadingProduct: false, // 查询商品信息
    loadingGameInfo: false, // 查询游戏信息
    loadingGameDaily: false,
    creatingWish: false, // 创建许愿
    cancelingWish: false, // 取消许愿
    loadingIsNewComer: false, // 是否 是新人保障
    loadingGameLottery: false, // 获取奖品查询
    claimingRewards: false, // 批量领取奖励
    loadingTask: false, // 查询任务列表
    loadingTaskDetail: false, // 查询任务详情
    computingTask: false,
    loadingProp: false, // 道具
    usingPropPossibility: false,
  },

};

export default function main(state = INITIAL_STATE, action: Action): IMainReducer {
  const { type, payload } = action;

  return produce<IMainReducer>(state, (draftState) => {
    switch (type) {
      //通用改变数据
      case Command.commonChange:
        return immerUtil.commonChange(draftState, { ...payload, key: 'main' });

      //初始化
      case Command.init:
        draftState.isReady = true;
        for (let propKey in payload.main) {
          //@ts-ignore 这里处理的不够好.
          draftState[propKey] = payload.main[propKey];
        }
        return draftState;

      //重置
      case Command.clean:
        for (let propKey in INITIAL_STATE) {
          //@ts-ignore 这里处理的不够好.
          draftState[propKey] = INITIAL_STATE[propKey];
        }
        return draftState;
    }
  });
}

//create by moon https://github.com/creasy2010/moon
